<?php
/**
 * 下载类（需要开启缓冲）
 *
 * @author fakis738@gmail.com
 * @link http://www.woophp.com/manual/class/download.lib.php
 * @version $id download.lib.php v0.1 2011-12-03$
 */
class Download_library
{
	protected $mime = 'application/octet-stream';
	protected $ext = '';
	protected $filename = '';
	
    public function __construct($cfg=NULL)
    {
    	$this->mimes = array();
    	if($cfg === NULL)
    	{
    		$cfg = new Config();
    		$this->mimes = $cfg->item('mimes');
    	}
    	if(is_object($cfg))
    	{
    		$this->mimes = $cfg->item('mimes');
    	}
    }
    
    /**
     * 下载文件名重命名
     * @access protected
     * @param string $filename
     * @return void
     */
    protected function setFilename($filename)
    {
    	$this->filename = $filename ? $filename : date('YmdHis');
    	$f = explode('.', $this->filename);
    	$this->ext = end($f);
    	if($this->ext && isset($this->mimes[$this->ext]))
    	{
    		$this->mime = is_array($this->mimes[$this->ext]) ? $this->mimes[$this->ext][0] : $this->mimes[$this->ext];
    	}
    }
    
    /**
     * 强制下载文件
     * @access protected
     * @param source $data
     * @return void
     */
    protected function force($data)
    {
    	if(empty($this->mime) || empty($this->filename) || empty($data)) return FALSE;
    	
    	//注意IE与非IE下载方法区别
    	if(strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== FALSE)
		{
			header('Content-Type: '. $this->mime);
			header('Content-Disposition: attachment; filename='. $this->filename);
			header('Expires: 0');
			header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
			header('Content-Transfer-Encoding: binary');
			header('Pragma: public');
			header('Content-Length: '. strlen($data));
		}
		else
		{
			header('Content-Type: '. $this->mime);
			header('Content-Disposition: attachment; filename='. $this->filename);
			header('Content-Transfer-Encoding: binary');
			header('Expires: 0');
			header('Pragma: no-cache');
			header('Content-Length: '. strlen($data));
		}
		
		exit($data);
    }
    
    /**
     * 下载文件
     * @access public
     * @param string $path
     * @param string $filename
     * @return void
     */
    public function file($path, $filename = '')
    {
    	if(empty($filename))
    	{
    		$filename = basename($path);
    	}
    	$this->setFileName($filename);
    	
    	$f = @fopen($path, 'r');
    	$data = @fread($f, filesize($path));
    	@fclose($f);
    	
    	return $this->force($data);
    }
    
    /**
     * 打包文件下件
     * @access public
     * @param array $paths
     * @param string $filename
     * @return void
     */
    public function zip($path, $filename = '')
    {
    	//检测是否支持ZipArchive库
    	if(!class_exists('ZipArchive')) die('Does not support the package to download');
    	
    	if(empty($filename))
    	{
    		$filename = basename($path);
    	}
    	$this->setFileName($filename);
    	
    	$zip = new ZipArchive;
    	$res = $zip->open($filename, ZipArchive::CREATE);
    	if($res === TRUE)
    	{
    		foreach($paths as $val)
    		{
    			$zip->addFile($val);
    		}
    		$zip->close();
    	}
    	
    	//载入打包文件后，将该文件删除
    	$data = @readfile($filename);
    	@unlink($filename);
    	
    	return $this->force($data);
    }
    
    /**
     * 压缩文件下载，使用Zlib库（需要Zlib库支持），以单个文件高压缩
     * @access public
     * @param string $path
     * @param string $filename
     * @return void
     */
    public function gz($path, $filename = '')
    {
    	//检测是否支持Zlib库
    	if(!function_exists('gzopen')) die('Does not support the package to download');
    	
    	if(empty($filename))
    	{
    		$filename = basename($path);
    	}
    	$this->setFileName($filename);
    	
    	$gz = gzopen($filename, 'w9');
    	gzwrite($gz, file_get_contents($path));
    	gzclose($gz);
    	
    	//载入压缩文件后，将该文件删除
    	$data = @readfile($filename);
    	@unlink($filename);
    	
    	return $this->force($data);
    }
}
